<?php
session_start();
include "../service/userService.php";

if(isset($_SESSION["username"][1])){
	$result = findUserBy($_SESSION["username"][1]);
	$row = ConnectionManager::getInstance()->fetch($result);
	
	$fullName = $_POST["fullName"];
	$dob = $_POST["dob"];
	$phoneNumber = $_POST["phoneNumber"];
	$email = $_POST["email"];
	// change information 
	if(!isset($_POST["check"])){
		if(editUser($_SESSION["username"][1], $fullName, $dob, $email, $phoneNumber, $row["USER_PASS"])){
			Header("Location: ../pc/user-account.php?msg=ok");		
		} else{
			Header("Location: ../pc/user-account.php?msg=no");
		}
	} else { // change information and password
		if($row["USER_PASS"] != md5($_POST["pwd"])){
			Header("Location: ../pc/user-account.php?msg=notmatch");
			exit;
		}
		
		$newPass = $_POST["newPassword"];
		if(editUser($_SESSION["username"][1], $fullName, $dob, $email, $phoneNumber, md5($newPass))){
			Header("Location: ../pc/user-account.php?msg=ok");		
		} else{
			Header("Location: ../pc/user-account.php?msg=no");
		}
	}
} else {
	Header("Location: ../pc/user-account.php");
}

?>